define(['moment'], function (moment) {
    return ['$scope', '$filter', '$mdDialog', 'config', 'MMWService', '$calculater', '$gallery', '$timeout',
        function ($scope, $filter, $mdDialog, config, $MMWService, $calculater, $gallery, $timeout) {
            $scope.init = function () {
                $scope.condition = {
                    mes_no: ''
                }
                $scope.datalist = [];
            };

            $scope.back = function () {
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('home', true);
            };

            // 确认
            $scope.confirm = function () {
                if ($scope.datalist.length == 0) {
                    $scope.$root.showAlert($filter('translate')('common.msg.plz_select_one'));
                    return;
                }
                // 检查小数位数
                var flag = true;
                for (var i = 0; i < $scope.datalist.length; i++) {
                    var tempUseQty = $scope.datalist[i].qty;
                    var qtyIndex = tempUseQty.indexOf(".") + 1;
                    var qtyCount = tempUseQty.length - qtyIndex;
                    if (qtyCount > 6) {
                        flag = false;
                        break;
                    }
                }
                if(!flag){
                    $scope.$root.showAlert($filter('translate')('B1602.msg.max_six_decimal'));
                    return;
                }
                var content = {
                    list: $scope.datalist
                }
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_MAT.material_back_deduct_process',
                    content: content,
                    success: function (data) {
                        $scope.$root.showToast($filter('translate')('common.msg.success'));
                        $scope.search();
                    },
                    error: function (data) {
                        $scope.$root.showAlert(data.Message);
                    },
                });

            };

            //查詢 - MES单号開窗
            $scope.loadMesNo = function () {
                $MMWService.showQueryListPNIG({
                    page_mode: 'MES_NO_GET',
                    condition: [
                        { name: $filter('translate')('B1602.queryList.mo_no'), column_name: 'b.WO_NO', query_mode: '2', value: '' },
                        { name: $filter('translate')('B1602.queryList.op_no'), column_name: 'b.OP_NO', query_mode: '2', value: '' }
                    ],
                    header: [
                        { label: $filter('translate')('B1602.queryList.mes_no'), code: 'mesno' },
                        { label: $filter('translate')('B1602.queryList.mo_no'), code: 'wo_no' },
                        { label: $filter('translate')('B1602.queryList.op_no'), code: 'op_no' },
                        { label: $filter('translate')('B1602.queryList.create_date'), code: 'createdate' }
                    ],
                    selectCode: $scope.condition.mes_no,
                    confirm: function (selectItem) {
                        $scope.condition.mes_no = selectItem.mesno;
                    }
                });
            };

            $scope.input_use_qty = function (event, item) {
                var target = event.currentTarget;
                angular.element(target).addClass('is-focus');
                $calculater.open(function (num, dialogNum) {
                    angular.element(target).removeClass('is-focus');
                    item.qty = num;
                    dialogNum.hide();
                },
                function (dialogNum) {
                    angular.element(target).removeClass('is-focus');
                    dialogNum.hide();
                },
                {
                    defaultNum: item.qty,
                    decimal: true
                });
            }

            //查詢 - 確認
            $scope.search = function () {
                if (!$scope.condition.mes_no) {
                    $scope.$root.showAlert($filter('translate')('B1602.msg.plz_select_mes_no'));
                    return;
                }
                var content = {
                    mes_no: $scope.condition.mes_no,
                }
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_MAT.material_back_deduct_get',
                    content: content,
                    success: function (data) {
                        if (data.list.length == 0) {
                            $scope.datalist = [];
                            $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                        } else {
                            $scope.datalist = data.list;
                        }
                    }
                });
            };

        }
    ];
});
